<?php
namespace Manage\Controller;
use Think\Controller;

class AdminSGalleryController extends Controller {

    private $model_name = 'SGallery';

    public function add_s_gallery(){
        if(!I('post.data')){
            exit;
        }
        $s_id = I('post.s_id');
        $model = D($this->model_name);
        $datas = array_filter(explode(',', $_POST['data']));
        foreach($datas as $data){
            $data_array = array_filter(explode('&', $data));
            $data = array();
            $data['s_type'] = $data_array[0];
            $data['s_id'] = $s_id;
            $data['img_type'] = $data_array[1];
            $data['path'] = $data_array[2];
            $one = $model->where($data)->find();
            if(!$one){
                $model->add($data);
            }
        }
        echo json_encode(array('status'=>1));
    }

    public function  del_s_gallery(){
        $id = I('post.id');
        if(!$id){
            exit;
        }
        $model = D($this->model_name);
        $model->delete($id);
    }

    public function get_s_gallery_by_s(){
        $s_type = I('get.s_type');
        $s_id = I('get.s_id');
        if(!$s_id){
            exit;
        }
        $model = D($this->model_name);
        $map['s_type'] = array('eq', $s_type);
        $map['s_id'] = array('eq', $s_id);
        $list = $model->where($map)->order('id desc')->select();
        echo json_encode($list);
    }

    public function download_img(){
        $path = I('get.path');
        header("Content-type: octet/stream");
        header('Content-Disposition: attachment; filename="'.basename($path).'"');
        header("Content-Length:".filesize('.'.$path));
        readfile('.'.$path);
    }

}